package View;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;


import Model.MonteCarloSimulation;
import Model.Option;
import Model.RunMonteCarloSimulation;

public class Window extends JFrame{

	private static final long serialVersionUID = 3603982671468152655L;
	private JPanel innerLeftPan;
	private JPanel leftPan;
	private JPanel rightPan;
	private JPanel innerRightPan;
	private JScrollPane scroll;
	private JSplitPane split;
	private JLabel[] label;
	private JComboBox[] cb;
	private JComboBox nb;
	private JLabel titleResult;
	private JLabel[] results;
	private JLabel[] elapsedTime;
	private JProgressBar progress;
	private JButton button;
	private MonteCarloSimulation monteCarlo;
	
	private MonteCarloSimulation getMonteCarlo(){
		return monteCarlo;
	}
	
	public Window(MonteCarloSimulation monteCarlo,String title){
		super(title);
		this.monteCarlo=monteCarlo;
		
		progress = new JProgressBar();
		button = new JButton("OK");	
		titleResult = new JLabel("Results: ");
		
		setPanel();
		setLabel();
		setComboBox(this.monteCarlo.getOptions());
		setLeftInnerPanel();
		setRightInnerPanel();
		
		ActionListener actionListener = new ActionListener() {
		      public void actionPerformed(ActionEvent actionEvent) {
		    	  Option option = new Option((String)cb[0].getSelectedItem(), (double)cb[1].getSelectedItem(),(double)cb[2].getSelectedItem(),(double)cb[3].getSelectedItem(),(double)cb[4].getSelectedItem(),(double)cb[5].getSelectedItem(),(double)cb[6].getSelectedItem(),(int)cb[7].getSelectedItem(),(int)cb[8].getSelectedItem());
		    	  Thread t = new Thread(new RunMonteCarloSimulation(getMonteCarlo(),button,progress, results, elapsedTime, option,(int)nb.getSelectedItem()));
		    	  t.start();
		    	  button.setVisible(false);
		      }
		};
		
		button.addActionListener(actionListener);
	    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    this.setSize(800, 500);
	    this.setContentPane(split);
	    this.setLocationRelativeTo(null);
	    this.setResizable(false);
	    this.setVisible(true);
	}
	
	public void setPanel(){
		innerLeftPan = new JPanel(new GridLayout(0,2));
		leftPan = new JPanel(new BorderLayout());
		leftPan.add(innerLeftPan, BorderLayout.CENTER);
		
		leftPan.add(button,BorderLayout.PAGE_END);
		
		rightPan = new JPanel(new BorderLayout());
		innerRightPan = new JPanel(new GridLayout(0,1));
		scroll = new JScrollPane(innerRightPan);
		rightPan.add(scroll, BorderLayout.CENTER);
		
		rightPan.add(titleResult, BorderLayout.PAGE_START);
		rightPan.add(progress, BorderLayout.PAGE_END);
		
		split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftPan,rightPan);
		
	}
	
	public void setLabel(){
		
		label = new JLabel[10];
		results = new JLabel[10];
		elapsedTime = new JLabel[10];
		
		for(int i=0;i<10;i++){
			results[i] = new JLabel("");
			elapsedTime[i] = new JLabel("");
		}
		
		label[0] = new JLabel("Option: ");
		label[1] = new JLabel("Current price of the underlying:");
		label[2] = new JLabel("Strike price: ");
		label[3] = new JLabel("Maturity: ");
		label[4] = new JLabel("Risk-free interest rate: ");
		label[5] = new JLabel("Detention fee rate option: ");
		label[6] = new JLabel("Price volatility of the underlying: ");
		label[7] = new JLabel("Number of steps: ");
		label[8] = new JLabel("Number of Simulations: ");
		label[9] = new JLabel("Do this (n times): ");
	}
	
	public void setComboBox(ArrayList<Option> options){
		
		cb = new JComboBox[9];
		nb = new JComboBox();
		
		for(int i=1;i<=10;i++){
			nb.addItem(i);
		}
		
		for(int i=0; i<9;i++){
			cb[i] = new JComboBox();
		}
		
		cb[0].addItem("call");
		cb[0].addItem("put");
		for(int i=1; i<options.size();i++){
			cb[1].addItem(options.get(i).getS());
			cb[2].addItem(options.get(i).getX());
			cb[3].addItem(options.get(i).getT());
			cb[4].addItem(options.get(i).getR());
			cb[5].addItem(options.get(i).getB());
			cb[6].addItem(options.get(i).getV());
			cb[7].addItem(options.get(i).getnSteps());
			cb[8].addItem(options.get(i).getnSimulations());
		}
	}
	
	public void setLeftInnerPanel(){
		for(int i=0;i<9;i++){
			innerLeftPan.add(label[i]);
			innerLeftPan.add(cb[i]);
		}
		innerLeftPan.add(label[9]);
		innerLeftPan.add(nb);
	}
	
	public void setRightInnerPanel(){
		for(int i=0;i<10;i++){
			innerRightPan.add(results[i]);
			innerRightPan.add(elapsedTime[i]);
		}
	}
	
}
